package com.xjcs.saplingplan.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.xjcs.saplingplan.databinding.ItemChatMsgBinding
import com.xjcs.saplingplan.model.bean.ChatMessage

class MessageAdapter(private val messages: List<ChatMessage>) :
    RecyclerView.Adapter<MessageAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        ViewHolder(
            ItemChatMsgBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        ))

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val message = messages[position]
        holder.binding.tvContent.text = message.content
        holder.binding.root.setBackgroundColor(
            if (message.isLocal) 0xFFE3F2FD.toInt() else 0xFFFFF8E1.toInt()
        )
    }

    override fun getItemCount() = messages.size

    class ViewHolder(val binding: ItemChatMsgBinding) :
        RecyclerView.ViewHolder(binding.root)
}